﻿using System.Collections.Generic;
using System.Data.Common;
using System.Data.EntityClient;
using System.Data.Objects;
using System.Linq;
using System.Transactions;
using ASPItalia.ModelVirtualCasting.EntityFramework.Wrappers;

namespace ASPItalia.ModelVirtualCasting.EntityFramework
{
	public class BaseRepository<T> where T : class
	{
		public BaseRepository(IContextContainer container)
		{
			Context = container.Current;
			CurrentObjectSet = CreateObjectSet();
			Commands = new List<EntityCommand>();
		}

		internal BaseRepository(IIncludeObjectSet<T> objectSet) {
			CurrentObjectSet = objectSet;
		}

		public IIncludeObjectSet<T> CurrentObjectSet { get; set; }
		protected IContextWrapper Context { get; set; }
		protected List<EntityCommand> Commands { get; set; }
		protected bool CreateNewContextAfterSaveChanges { get; set; }

		protected void SaveChanges()
		{
			Context.SaveChanges(Commands, CreateNewContextAfterSaveChanges);
		}

		internal ObjectSetWrapper<T> CreateObjectSet()
		{
			return new ObjectSetWrapper<T>(Context.CreateObjectSet<T>());
		}

		protected void SetLazyLoading(bool enabled)
		{
			Context.EnableLazyLoading = enabled;
		}
	}
}
